package com.competition.reception.controller;


import com.competition.commonutils.R;
import com.competition.reception.entity.ExaminationDetail;
import com.competition.reception.service.ExaminationDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 知识竞赛题目表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-05
 */
@RestController
@RequestMapping("/reception/examination-detail")
@CrossOrigin
@Api(description = "试题上传相关的接口")
public class ExaminationDetailController {


    @Autowired
    private ExaminationDetailService examinationDetailService;

    @GetMapping("getExaminationSubject/{examinationId}")
    @ApiOperation("根据竞赛id获取竞赛题目")
    private R getExaminationSubject(@PathVariable String examinationId){
        List<ExaminationDetail> examinationDetails=examinationDetailService.getExaminationSubject(examinationId);
        return R.ok().data("examinationDetails",examinationDetails);
    }

    @ApiOperation("上传竞赛文件,excel文件,返回全部题目数量，竞赛编号，必须将竞赛编号返回，上传的内容，")
    @PostMapping("uploadToptic")
    private R uploadToptic(MultipartFile file){
        try{
            R result= examinationDetailService.uploadToptic(file);
            return R.ok().data("result",result);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().data("msg","上传竞赛失败");
        }

    }

    @DeleteMapping("deleteToptic/{id}")
    @ApiOperation("根据id删除对应的竞赛题目")
    private R deleteToptic(@PathVariable String id){
        examinationDetailService.removeById(id);
        return R.ok();
    }

    @DeleteMapping("cancelUpload/{id}")
    @ApiOperation("取消上传")
    private R cancelUpload(@PathVariable String id){
        Map<String,Object> deleteMap=new HashMap<>();
        deleteMap.put("examination_id",id);
        examinationDetailService.removeByMap(deleteMap);
        return R.ok();
    }


}

